package com.example.springbootwebrestfulcrud.controller;

import com.example.springbootwebrestfulcrud.exception.UserNotFoundRxception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: luzhanshi
 * @Date: 2021/2/27 - 02 - 27 - 0:01 
 * @Description: com.example.springbootwebrestfulcrud.controller
 * @version: 1.0
 */
@ControllerAdvice
public class MyExceptHandder {
    //1.浏览器，以及其他客户端返回的都是json数据
   /* @ResponseBody
    @ExceptionHandler(UserNotFoundRxception.class)
    public Map handelException(Exception e) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "-1");
        map.put("message", e.getMessage());
        return map;
    }*/

    @ExceptionHandler(UserNotFoundRxception.class)
    public String handelException(Exception e, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", "-1");
        map.put("message", e.getMessage());
        request.setAttribute("ext",map);
        //传入我们自己的状态码  4** 5**
        request.setAttribute("javax.servlet.error.status_code",500);
        //转发到error
        return "forward:/error";
    }


}
